public class MyLinkedList {
    static class ListNode {
        public int val;
        public ListNode next;
        public ListNode prev;
        public ListNode(int val) {
            this.val = val;
        }
    }
    public static ListNode head;
    public static ListNode last;
    public int size() {
        ListNode cur = head;
        int size = 0;
        while (cur != null) {
            size++;
            cur = cur.next;
        }
        return size;
    }
    public void display() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
    }
    public boolean contains (int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if (head == null) {
            head = last = node;
        }else{
            node.next = head;
            head.prev = node;
            head = node;
        }
    }
    public void addLast(int data) {
        ListNode node =new ListNode(data);
        if (head == null) {
            head = last = node;
        }else {
            last.next = node;
            node.prev = last;
            last = node;
        }
    }
    //方法一
    public void  addIndex(int data,int index) {
        if (index < 0 || index > size()) {
            throw new IllegalPositionException("位置不合法");
        }
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == size()) {
            addLast(data);
            return;
        }
        ListNode cur = head;
        ListNode node = new ListNode(data);
        int count = 0;
        while (cur != null) {
            if (index == count) {
                cur.prev.next = node;
                node.prev = cur.prev;
                cur.prev = node;
                node.next = cur;
                return;
            }
            cur = cur.next;
            count++;
        }
    }
    //方法二
    /*public void addIndex(int data, int index) {
        ListNode cur = head;
        if (index < 0 || index > size()) {
            throw new IllegalPositionException("位置不合法");
        }
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == size()) {
            addLast(data);
            return;
        }
        ListNode A = new ListNode(data);
        for (int i = 0; i < index - 1; i++) {
            cur = cur.next;
        }
        A.prev = cur;
        A.next = cur.next;
        cur.next = A;
        A.next.prev = A;
    }*/
    public void remove (int key){
        ListNode cur = head;
        // 判断是否为空链表
        if (head == null) {
            return;
        }
        // 单独处理单节点
        if (cur.prev == null && cur.next == null) {
            if (cur.val == key) {
                head = last = null;
            }else {
                return;
            }
        }
        while (cur != null) {

            if (cur.val == key) {
                // 处理头节点
                if (cur == head) {
                    head = head.next;
                    head.prev = null;
                    return;
                }
                // 处理尾节点
                if (cur == last) {
                    last = last.prev;
                    last.next = null;
                    return;
                }
                // 处理中间的一般节点
                cur.prev.next = cur.next;
                cur.next.prev = cur.prev;
                return;
            }
            cur = cur.next;
        }
    }
    public void removeAllKey (int key){
        ListNode cur = head;
        // 判断是否为空链表
        if (head == null) {
            return;
        }
        // 单独处理单节点
        if (cur.prev == null && cur.next == null) {
            if (cur.val == key) {
                head = last = null;
            }else {
                return;
            }
        }
        while (cur != null) {

            if (cur.val == key) {
                // 处理头节点
                if (cur == head) {
                    head = head.next;
                    // 有可能全为目标值使得head最后为空
                    if (head != null) {
                        head.prev = null;
                    }else {
                        head = last = null;
                    }
                }
                // 处理尾节点
                if (cur == last) {
                    last = last.prev;
                    last.next = null;
                }
                if (cur != head && cur != last) {
                // 处理中间的一般节点
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                }
            }
            cur = cur.next;
        }
    }
         
}
